package com.maistars.spider.service.converter;

import com.alibaba.fastjson.JSONObject;
import com.maistars.spider.service.domain.rule.FieldRule;
import com.maistars.spider.service.parser.policy.ParserPolicyType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author dhr
 * @date 2021/10/10 下午2:45
 */
public class FieldRuleConverter {

    public static List<FieldRule> htmlXpathConfigToFieldRule(String htmlXpathConfig) {
        List<FieldRule> fieldRules = new ArrayList<>();
        JSONObject configObject = JSONObject.parseObject(htmlXpathConfig);
        for (Map.Entry<String, Object> entry : configObject.entrySet()) {
            JSONObject childObject = (JSONObject) entry.getValue();
            FieldRule fieldRule = FieldRule.builder()
                    .fieldName(entry.getKey())
                    .rule(childObject.getString("selectors"))
                    .parseType(ParserPolicyType.XPATH.getValue())
                    .build();

            JSONObject filterConfig = new JSONObject();

            JSONObject orignalConfig = new JSONObject(childObject);
            filterConfig.put("orignalConfig", orignalConfig);

            JSONObject extractConfig = new JSONObject();
            extractConfig.put("useText", childObject.getBoolean("useText"));
            extractConfig.put("useHtml", childObject.getBoolean("useHtml"));
            extractConfig.put("useOuterHtml", childObject.getBoolean("useOuterHtml"));
            extractConfig.put("useOwnText", childObject.getBoolean("useOwnText"));
            extractConfig.put("useAttr", childObject.getBoolean("useAttr"));
            extractConfig.put("useRandom", childObject.getBoolean("useRandom"));

            JSONObject extractExtendConfig = new JSONObject();
            extractExtendConfig.put("defaultValue", childObject.getString("defaultValue"));
            extractExtendConfig.put("minRandomValue", childObject.getString("minRd"));
            extractExtendConfig.put("maxRandomValue", childObject.getString("maxRd"));
            extractExtendConfig.put("maxRandomValue", childObject.getString("minRd"));
            extractExtendConfig.put("minRandomDate", childObject.getString("minRdtStr"));
            extractExtendConfig.put("maxRandomDate", childObject.getString("maxRdtStr"));
            extractExtendConfig.put("randomStrValue", childObject.getString("txtRd"));
            extractExtendConfig.put("randomType", childObject.getString("randType"));
            extractExtendConfig.put("gatherImg", childObject.getBoolean("useImg"));
            extractExtendConfig.put("gatherLink", childObject.getBoolean("useLink"));
            extractExtendConfig.put("concatDelimiter", childObject.getString("concatDelimiter"));
            extractExtendConfig.put("imgAttr", childObject.getString("imgAttr"));
            extractExtendConfig.put("nodeTag", childObject.getString("nodeTag"));
            extractExtendConfig.put("attrName", childObject.getString("attrName"));

            extractConfig.put("extendConfig", extractExtendConfig);

            filterConfig.put("extract", extractConfig);

            fieldRule.setProcessConfig(filterConfig.toJSONString());
            fieldRules.add(fieldRule);
        }
        return fieldRules;
    }


    public static String fieldRuleToHtmlXpathConfig(List<FieldRule> fieldRules) {
        JSONObject configObject = new JSONObject();

        for (FieldRule fieldRule : fieldRules) {
            JSONObject filterConfig = JSONObject.parseObject(fieldRule.getProcessConfig());
            JSONObject orignalConfig = filterConfig.getJSONObject("orignalConfig");

            JSONObject childFieldObject = JSONObject.parseObject("{\n" +
                    "  \"id\": \"\",\n" +
                    "  \"name\": \"\",\n" +
                    "  \"required\": false,\n" +
                    "  \"useHtml\": true,\n" +
                    "  \"useOuterHtml\": false,\n" +
                    "  \"useLink\": false,\n" +
                    "  \"mutiValue\": true,\n" +
                    "  \"pageMutiValue\": false,\n" +
                    "  \"isMeta\": false,\n" +
                    "  \"selectors\": \"\",\n" +
                    "  \"fieldValues\": [],\n" +
                    "  \"defaultValue\": \"\",\n" +
                    "  \"valueType\": \"text\",\n" +
                    "  \"attrName\": \"\",\n" +
                    "  \"matchRegex\": \"\",\n" +
                    "  \"matchReplace\": \"\",\n" +
                    "  \"matchGp\": \"\",\n" +
                    "  \"matchSplit\": \"\",\n" +
                    "  \"replaces\": [],\n" +
                    "  \"dataDealVos\": [],\n" +
                    "  \"cleanHtml\": false,\n" +
                    "  \"keepAlt\": 0,\n" +
                    "  \"htmlEles\": \"\",\n" +
                    "  \"groupSelect\": true,\n" +
                    "  \"fileTypes\": \"1\",\n" +
                    "  \"fileUrlPattern\": \"\",\n" +
                    "  \"concatDelimiter\": \"\",\n" +
                    "  \"useImg\": false,\n" +
                    "  \"imgAttr\": \"\",\n" +
                    "  \"delaTxt\": 0,\n" +
                    "  \"dela\": \"0\",\n" +
                    "  \"delAs\": \"\",\n" +
                    "  \"delAllImg\": \"0\",\n" +
                    "  \"delImg\": \"\",\n" +
                    "  \"delBlank\": 0,\n" +
                    "  \"pType\": 0,\n" +
                    "  \"preTxt\": \"\",\n" +
                    "  \"tailTxt\": \"\",\n" +
                    "  \"randType\": \"0\",\n" +
                    "  \"minRd\": \"0\",\n" +
                    "  \"maxRd\": \"0\",\n" +
                    "  \"minRdtStr\": \"\",\n" +
                    "  \"maxRdtStr\": \"\",\n" +
                    "  \"txtRd\": \"\",\n" +
                    "  \"mutiPage\": 0,\n" +
                    "  \"pageMode\": 1,\n" +
                    "  \"pageSelector\": \"\",\n" +
                    "  \"pageSave\": \"1\",\n" +
                    "  \"pageConcat\": \"\",\n" +
                    "  \"maxPage\": \"0\",\n" +
                    "  \"maxCrawl\": 0,\n" +
                    "  \"urlContain\": \"\",\n" +
                    "  \"lenType\": \"0\",\n" +
                    "  \"lenLimit\": \"0\",\n" +
                    "  \"filterType\": \"1\",\n" +
                    "  \"noInclude\": \"\",\n" +
                    "  \"mustInclude\": \"\",\n" +
                    "  \"renameFile\": true\n" +
                    "}");
            if(orignalConfig!=null){
                childFieldObject = new JSONObject(orignalConfig);
            }
            childFieldObject.put("id", fieldRule.getFieldId());
            childFieldObject.put("name", fieldRule.getFieldName());
            childFieldObject.put("selectors", fieldRule.getRule());

            configObject.put(String.valueOf(fieldRule.getFieldId()),childFieldObject);
        }
        return configObject.toJSONString();
    }
}
